package cn.sh.library.mgts.config;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkProcessor;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.cluster.metadata.AliasAction.Add;
import org.elasticsearch.common.network.InetAddresses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

import cn.sh.library.mgts.framework.factory.ESClientSpringFactory;
import cn.sh.library.mgts.service.impl.AsyncServiceImpl;

@Configuration
@ConfigurationProperties(prefix = "elasticsearch")
public class EsConfig {
	private String user;
	
	private String password;
	
    private List<String> urls;

    private Integer maxConnNum;

    private Integer maxPerRouter;

	private static Logger logger = LoggerFactory.getLogger(EsConfig.class);
    @Bean
    public HttpHost[] httpHost(){
		List<HttpHost> hosts = new ArrayList<HttpHost>();
		urls.forEach(urlstring ->{
			URL url;
			try {
				url = new URL(urlstring);
				hosts.add(new HttpHost(url.getHost(), url.getPort(), url.getProtocol()));
			} catch (MalformedURLException e) {
				e.printStackTrace();
				logger.error(e.getMessage());
			}
		});
			
        return hosts.toArray(new HttpHost[hosts.size()]);
    }

    @Bean(initMethod="init",destroyMethod="close")
    public ESClientSpringFactory getFactory(){
        return ESClientSpringFactory.
                build(httpHost(), maxConnNum, maxPerRouter, user, password);
    }

    @Bean
    @Scope("singleton")
    public RestClient getRestClient(){
        return getFactory().getClient();
    }

    @Bean(value="highLevelClient")
    @Scope("singleton")
    public RestHighLevelClient getRHLClient(){
        return getFactory().getRhlClient();
    }

    @Bean(value="bulkProcessorBuilder")
    public BulkProcessor.Builder getBulkProcessorBuilder() {
    	return getFactory().getBulkProcessorBuilder();
    }
    
    @Bean(value="bulkProcessor")
    public BulkProcessor getBulkProcessor() {
    	return getFactory().getBulkProcessor();
    }

	public List<String> getUrls() {
		return urls;
	}

	public void setUrls(List<String> urls) {
		this.urls = urls;
	}

	public Integer getMaxConnNum() {
		return maxConnNum;
	}

	public void setMaxConnNum(Integer maxConnNum) {
		this.maxConnNum = maxConnNum;
	}

	public Integer getMaxPerRouter() {
		return maxPerRouter;
	}

	public void setMaxPerRouter(Integer maxPerRouter) {
		this.maxPerRouter = maxPerRouter;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
    
}
